package com.fire.control.system.module.warning.dao;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.fire.control.system.entity.WarningInfo;
import com.fire.control.system.statics.Type_SureWarning;
/**
 * 
 * @ClassName:  WarningInfoRepository
 * @Description:  报警设备持久层
 * @author:  xiechengguang
 * @date:  2017年5月6日 上午9:36:46
 *
 */
@Repository 
public interface WarningInfoRepository extends JpaRepository<WarningInfo, String>, JpaSpecificationExecutor<WarningInfo> {
//	@Query("SELECT DISTINCT w FROM WarningInfo w JOIN w.targetDevice d JOIN d.company c where w.warningType = 'battery_fault' AND c.id = :companyId")
//	public List<WarningInfo> batteryWarnings(@Param("companyId") String companyId);
//	
//	@Query("SELECT DISTINCT w FROM WarningInfo w JOIN w.targetDevice d JOIN d.company c where w.warningType = 'fire_alarm' AND c.id = :companyId")
//	public List<WarningInfo> fireWarnings(@Param("companyId") String companyId);
//	
//	@Query("SELECT DISTINCT w FROM WarningInfo w JOIN w.targetDevice d JOIN d.company c where w.warningType = 'device_fault' AND c.id = :companyId")
//	public List<WarningInfo> deviceFault(@Param("companyId") String companyId);
	
	@Query("from WarningInfo w where w.targetDevice.id = :deviceId")
	public WarningInfo findByDeviceId(@Param("deviceId") String deviceId);
	
	@Transactional
	@Modifying
	@Query("update WarningInfo w set w.status = false where w.id = :id")
	public int updateWarningInfoStatus(@Param("id") String id);
	
	
	/**
	 * @author: xiechengguang 
	 * @Title: updateWarningType   
	 * @Description: 更新当前状态为处理中以及报警类型  
	 * @param:        
	 * @return: int      
	 * @throws
	 */
	/*@Transactional
	@Modifying
	@Query("update WarningInfo w set w.currentStatus = 'proccessing', w.warningType = :warningType where w.id = :id")
	public int updateWarningType(@Param("id") String id, @Param("warningType") String warningType);*/
	
	/**火灾报警--完成报警流程*/
/*	@Transactional
	@Modifying
	@Query("update WarningInfo w set w.currentStatus = 'complete' where w.id = :id")
	public int finishWarning(@Param("id") String id);*/
	
	/**火灾报警--接警*/
	@Transactional
	@Modifying
	@Query("update WarningInfo w set w.currentStatus = 'receive' where w.id = :id")
	public int receive(@Param("id") String id);
	
	/**
	 * @author: xiechengguang 
	 * @Title: updateComplete   
	 * @Description: 确认报警类型为误报,更新当前状态为已完成
	 * @param:        
	 * @return: int      
	 * @throws
	 */
	@Transactional
	@Modifying
	@Query("update WarningInfo w set w.currentStatus = 'complete', w.sureWarning = :sureWarning where w.id = :id")
	public int updateComplete(@Param("id") String id, @Param("sureWarning") Type_SureWarning sureWarning);
	
	/**
	 * @author: xiechengguang 
	 * @Title: updateApplyfor   
	 * @Description: 确认报警类型为故障申报,更新当前状态为申请维保 
	 * 并将处理详细的处理类型标为：dispose
	 * @param:        
	 * @return: int      
	 * @throws
	 */
	/*@Transactional
	@Modifying
	@Query("update WarningInfo w,WarningHandle h set w.currentStatus = 'applyfor', w.sureWarning = :sureWarning, h.solutionType = 'dispose' where h.warningInfo = w.id AND w.id = :id")
	public int updateApplyfor(@Param("id") String id, @Param("sureWarning") Type_SureWarning sureWarning);*/
	
	/**
	 * @author: xiechengguang 
	 * @Title: updateProcessing   
	 * @Description: 更新当前状态为处理中，只有火灾报警才有这个功能入口  
	 * @param:        
	 * @return: int      
	 * @throws
	 */
	@Transactional
	@Modifying
	@Query("update WarningInfo w set w.currentStatus = 'processing' where w.id = :id")
	public int updateProcessing(@Param("id") String id);
	
	/**
	 * @author: xiechengguang 
	 * @Title: ignoreWarning   
	 * @Description: 忽略误报  
	 * @param:        
	 * @return: int      
	 * @throws
	 */
	/*@Transactional wanbo注释
	@Modifying
	@Query("update WarningInfo w set w.solution = 'not_existed' where w.id = :id")
	public int ignoreWarning(@Param("id") String id);*/
	
	/**
	 * @author: xiechengguang 
	 * @Title: handleDeviceMaintence   
	 * @Description: 维保处理  
	 * @param:        
	 * @return: int      
	 * @throws
	 */
	/*wanbo 注释
	 * @Transactional
	@Modifying
	@Query("update WarningInfo w set w.solutionDesc = :solutionDesc, w.trustees = :trustees, w.endTime = :endTime where w.id = :id")
	public int handleDeviceMaintence(@Param("solutionDesc") String solutionDesc, @Param("trustees") String trustees, @Param("endTime") Date endTime, @Param("id") String id);*/
	
}
